# -*- coding: utf-8 -*-
import numpy as np

power_curves = {
    "GW90": {
        "wspd": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
        "power": [24, 98, 212, 371, 592, 884, 1253, 1659, 2077, 2500, 2500, 2500, 2500, 2500, 2500, 2500,
            2500, 2500, 2500, 2500, 2500, 2500, 2500],
        "height": 80,
        "rated_power": 2.5,
        "IEC": "II",
    },
    "GW100": {
        "wspd": [3, 4, 5, 6, 7, 8, 9, 10, 10.9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
        "power": [54, 136, 271, 471, 750, 1118, 1557, 2036, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,
             2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500],
        "height": 80,
        "rated_power": 2.5,
        "IEC": "III",
    },
    "GW103": {
        "wspd": [3, 4, 5, 6, 7, 8, 9, 10, 10.5, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
        "power": [54, 149, 296, 515, 819, 1218, 1693, 2200, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500,
             2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500],
        "height": 80,
        "rated_power": 2.5,
        "IEC": "I",
    },
    "GW70": {
        "wspd": [3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20, 20.5, 21, 21.5, 22, 22.5, 23, 23.5, 24, 24.5, 25],
        "power": [11, 31, 57, 89, 126, 170, 223, 287, 360, 445, 543, 653, 777, 910, 1048, 1189, 1332, 1477, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500],
        "height": 65,
        "rated_power": 1.5,
        "IEC": "I",
    },
    "GW77": {
        "wspd": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
        "power": [14, 71, 154, 271, 436, 657, 930, 1213, 1488, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500],
        "height": 65,
        "rated_power": 1.5,
        "IEC": "II",
    },
    "GW93": {
        "wspd": [2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19],
        "power": [3, 16, 40, 70, 106, 148, 199, 261, 334, 420, 519, 632, 758, 886, 1011, 1136, 1259, 1377, 1486, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500],
        "height": 85,
        "rated_power": 1.5,
        "IEC": "III",
    },
    "GW115": {
        "wspd": [2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19],
        "power": [37, 73, 120, 184, 265, 367, 491, 641, 818, 1022, 1253, 1517, 1777, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000],
        "height": 85,
        "rated_power": 2.0,
        "IEC": "low",
    },
    "EN110": {
        "wspd": [3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20, 20.5, 21, 21.5, 22, 22.5, 23, 23.5, 24, 24.5, 25],
        "power": [64, 111, 181, 259, 356, 474, 616, 784, 980, 1206, 1465, 1758, 2087, 2272, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300, 2300],
        "height": 80,
        "rated_power": 2.3,
        "IEC": "low",
    },
    "MY104": {
        "wspd": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
        "power": [20, 124, 275, 505, 816, 1236, 1729, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000],
        "height": 80,
        "rated_power": 2.0,
        "IEC": "low",
    },
    "H111": {
        "wspd": [3, 4, 5, 6, 7, 8, 9, 9.6, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
        "power": [21, 144, 334, 612, 998, 1486, 1914, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000],
        "height": 80,
        "rated_power": 2.0,
        "IEC": "low",
    },
    "G97": {
        "wspd": [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
        "power": [14, 94, 236, 438, 714, 1084, 1508, 1836, 1973, 1992, 1998, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 1676, 1234],
        "height": 78,
        "rated_power": 2.0,
        "IEC": "low",
    },
    "UP105": {
        "wspd": [3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20, 20.5, 21, 21.5, 22],
        "power": [35.8, 75.1, 125.6, 189.8, 263.1, 353.8, 474, 603.6, 768.7, 938.8, 1135, 1288.2, 1464.7, 1623.4, 1784.5, 1888.6, 1958.7, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000],
        "height": 80,
        "rated_power": 2.0,
        "IEC": "low",
    }
}


def interp_wspd(wspd1, wspd2, h1, h2, target_h, base_h, base_wspd):
    alpha = np.log(wspd2 / wspd1) / np.log(float(h2) / h1)
    wspd = base_wspd * np.power((float(target_h) / base_h), alpha)
    return wspd


def get_eqhour(turbine_model, wspd, rhoair=1.225):
    # alpha = np.log(wspd_100 / wspd_70) / np.log(100.0 / 70)
    # # index = 0 or 1
    # if para['height'] == 65:
    #     level = 70
    #     wspd = wspd_70 * np.power((65.0 / 70), alpha)
    # elif para['height'] == 85:
    #     level = 80
    #     wspd = wspd_80 * np.power((85.0 / 80), alpha)
    # 0.95尾流折减
    coe = (1 - (2.0 / 3) * (1 - rhoair / 1.225)) * 0.7 * 0.95
    # print len(wspd[(wspd<3) | (wspd>25)])
    para = power_curves.get(turbine_model)
    inter = np.interp(wspd, para['wspd'], para['power'], left=0.0)
    # print len(inter[inter==0])
    valid = inter * coe
    orig_len = len(inter)
    valid = valid[np.isfinite(valid)]
    valid_len = len(valid)
    eqhour = np.sum(valid) / ((float(valid_len) / orig_len) * para['rated_power'] * 1000.0)
    return round(eqhour)
